package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


//旅游攻略
@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStatisVORedisService statisVORedisService;

    //查询明细
    @GetMapping("/detail")
    public Object detail(String id) {
        Strategy strategy = strategyService.get(id);
        //增加阅读数
        statisVORedisService.viewnumIncrease(id,1);
        return JsonResult.success(strategy);
    }


    //查询全部
    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }

    //查询全部主题
    @GetMapping("/themes")
    public Object themes() {
        List<StrategyTheme> themes = strategyThemeService.list();
        return JsonResult.success(themes);
    }

    //攻略评论
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo) {
        //保存缺少冗余字段,调用Bean的工具类进行复制补全
        BeanUtils.copyProperties(userInfo, comment);
        //因为userInfo自身是没有userId,所以得手动设置进去
        comment.setUserId(userInfo.getId());
        //创建时间也是缺少的,需要补全
        comment.setCreateTime(new Date());
        strategyCommentService.save(comment);
        //评论数增加+1
        statisVORedisService.replaynumIncrease(comment.getStrategyId(),1);
        return JsonResult.success();
    }


    //显示评论分页
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo) {
        Page<StrategyComment> commentPage = strategyCommentService.query(qo);
        return JsonResult.success(commentPage);
    }

    //攻略评论点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, String sid, @UserParam UserInfo userInfo) {
        strategyCommentService.commentThumb(cid, sid, userInfo);
        return JsonResult.success();
    }

    //显示阅读数
    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        //获取攻略统计对象
        StrategyStatisVO strategy = statisVORedisService.getStrategyStatisVO(sid);
        return JsonResult.success(strategy);
    }


    //用户攻略收藏
    //用户需要登录才能收藏
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo) {
        Boolean ret = statisVORedisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }


    //点赞数统计
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo) {
        Boolean ret = statisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
}

